package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.ServiceAuditRecordDetailMapper;
import com.eastfair.venueservice.entity.ServiceAuditRecord;
import com.eastfair.venueservice.entity.ServiceAuditRecordDetail;
import com.eastfair.venueservice.entity.ServiceItemDetail;
import com.eastfair.venueservice.service.ServiceAuditRecordDetailService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.venueservice.service.ServiceItemDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 业务实现类
 * 服务审核记录详情
 * </p>
 *
 * @author lyw
 * @date 2022-07-13
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ServiceAuditRecordDetailServiceImpl extends SuperServiceImpl<ServiceAuditRecordDetailMapper, ServiceAuditRecordDetail> implements ServiceAuditRecordDetailService {
    @Autowired
    private ServiceItemDetailService serviceItemDetailService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ServiceAuditRecordDetail> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ServiceAuditRecordDetail model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        return R.successDef();
    }


    /**
     * 保存审核详情记录
     * @param serviceItemId,serviceAuditRecordId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveServiceAuditRecordDetail(Long serviceItemId,Long serviceAuditRecordId) {
        log.info("ServiceAuditRecordDetailServiceImpl +++++ saveServiceAuditRecordDetail - 保存审核详情记录 params={}", serviceItemId,serviceAuditRecordId);
        Boolean flag=Boolean.FALSE;
        //根据服务项id查询服务项详情信息
        List<ServiceItemDetail> serviceItemDetailList=  serviceItemDetailService.getServiceItemDetailByServiceItemId(serviceItemId);
        if(Objects.nonNull(serviceItemDetailList) && serviceItemDetailList.size()>0){
          for(ServiceItemDetail serviceItemDetail:serviceItemDetailList){
              ServiceAuditRecordDetail serviceAuditRecordDetail = BeanUtil.toBean(serviceItemDetail, ServiceAuditRecordDetail.class);
              serviceAuditRecordDetail.setId(null);
              serviceAuditRecordDetail.setServiceAuditRecordId(serviceAuditRecordId);
              //保存服务详情
              flag=this.save(serviceAuditRecordDetail);
          }
        }
        return flag;
    }
}
